<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Mod_subjects extends CI_Controller {
	private $per_page = 5;
	private $total_rows = '';
	
	function __construct() {
		parent::__construct();
	}
	
	function index() {
	}
	
	function get_tutor() {
		$offset = $this->uri->segment(2);
		echo $offset;
		$alias      = trim(preg_replace("/(-subjects([0-9]+))/", '', $this->security->xss_clean($this->uri->segment(1, 0)))); //cat duoi html
		$categories = $this->common_model->getone('subjects', array(
			'alias' => $alias
		));
		$mon        = $this->common_model->getdata('subjects', array(
			'publish' => 1
		));
		if (!empty($categories)) {
			$catid = ',' . $categories->id . ',';
			$this->db->like('subjects', $catid);
			$ketqua = $this->common_model->getdata('users', '', '', array(
				'id',
				'DESC'
			), array(
				$this->per_page,
				$offset
			));
			$this->db->like('subjects', $catid);
			$this->total_rows = $this->common_model->getnumber2('users', '', '', array(
				'id',
				'DESC'
			));
			if (!empty($ketqua)) {
				foreach ($ketqua as $giasu) {
					$danhgia[$giasu->id]            = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id
					));
					$binhluan[$giasu->id]           = $this->common_model->getnumber2('rating_des', array(
						'tutorid' => $giasu->id,
						'publish' => 1
					));
					$data2['onestar'][$giasu->id]   = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id,
						'onestar' => 1
					));
					$data2['twostar'][$giasu->id]   = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id,
						'twostar' => 1
					));
					$data2['threestar'][$giasu->id] = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id,
						'threestar' => 1
					));
					$data2['fourstar'][$giasu->id]  = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id,
						'fourstar' => 1
					));
					$data2['fivestar'][$giasu->id]  = $this->common_model->getnumber2('rating_star', array(
						'tutorid' => $giasu->id,
						'fivestar' => 1
					));
					$data2['numstar'][$giasu->id]   = $data2['onestar'][$giasu->id] + $data2['twostar'][$giasu->id] + $data2['fourstar'][$giasu->id] + $data2['fivestar'][$giasu->id] + $data2['threestar'][$giasu->id];
					$data2['percent1'][$giasu->id]  = $this->percent($data2['onestar'][$giasu->id], $data2['numstar'][$giasu->id]);
					$data2['percent2'][$giasu->id]  = $this->percent($data2['twostar'][$giasu->id], $data2['numstar'][$giasu->id]);
					$data2['percent3'][$giasu->id]  = $this->percent($data2['threestar'][$giasu->id], $data2['numstar'][$giasu->id]);
					$data2['percent4'][$giasu->id]  = $this->percent($data2['fourstar'][$giasu->id], $data2['numstar'][$giasu->id]);
					$data2['percent5'][$giasu->id]  = $this->percent($data2['fivestar'][$giasu->id], $data2['numstar'][$giasu->id]);
					$arrstar                        = array(
						$data2['onestar'][$giasu->id],
						$data2['twostar'][$giasu->id],
						$data2['threestar'][$giasu->id],
						$data2['fourstar'][$giasu->id],
						$data2['fivestar'][$giasu->id]
					);
					$sosao[$giasu->id]              = $this->checksao($arrstar);
				}
			} else {
				$danhgia  = '';
				$binhluan = '';
				$sosao    = '';
			}
			$data['title']   = $categories->title;
			$data['metades'] = $categories->metades;
			$data['metakey'] = $categories->metakey;
		} else {
			$this->load->view('404.php');
		}
		$this->phantrang();
		$data['content'] = $this->load->view('mod_subjects/views_subjects.php', array(
			'ketqua' => $ketqua,
			'mon' => $mon,
			'categories' => $categories,
			'danhgia' => $danhgia,
			'binhluan' => $binhluan,
			'sosao' => $sosao,
			'tong' => $this->total_rows
		), true);
		$this->load->view('index', $data);
	}
	
	function percent($num, $sum) {
		if ($num != 0) {
			$percent = ($num / $sum) * 100;
			return $percent;
		} else {
			return 0;
		}
	}
	
	function checksao($arrsao = array('')) {
		$max = max($arrsao);
		$i   = 0;
		foreach ($arrsao as $sao1) {
			$i++;
			if ($sao1 == $max) {
				$sao = $i;
			}
		}
		return $sao;
	}
	
	function phantrang() {
		$this->load->library('pagination');
		$config['base_url']    = base_url() . $this->uri->segment(1);
		$config['total_rows']  = $this->total_rows;
		$config['per_page']    = $this->per_page;
		$config['uri_segment'] = 2;
		$config['num_links']   = 12;
		$this->pagination->initialize($config);
	}
}
?>